package org.example.cicdi.service.impl.second.defaulttableandfield;


import org.example.cicdi.enums.EnumWhiteColumnAnnotation;
import org.example.cicdi.service.impl.second.AnnotationUtil;
import org.example.cicdi.service.impl.second.FieldAnnotationServiceSecond;
import org.example.codegenerate.modelext.WebVo.Enum.EnumJavaMappingDbType;
import org.example.codegenerate.modelext.WebVo.Enum.EnumPageFieldType;
import org.example.codegenerate.modelext.WebVo.annotation.Column.PageVo;
import org.example.codegenerate.modelext.WebVo.domian.column.ColumnVoDomain;

import org.example.codegenerate.modelext.WebVo.domian.table.WebModelVo;
import org.springframework.stereotype.Service;

import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.lang.reflect.Field;
import java.util.List;

@Service
public class DefaultPageVoAnnotatonImpleServiceSecond implements FieldAnnotationServiceSecond {

	@Override
	public String getCode() {
		return EnumWhiteColumnAnnotation.PageVo.getCode();
	}


	//这个方法中传入了WebModelVo，及WebModelVoList没有作用
	public ColumnVoDomain handleFieldVo(Field field, ColumnVoDomain column, WebModelVo webModelVo, List<WebModelVo> webModelVoList){
		PageVo vo = field.getAnnotation(PageVo.class);
		String fk="";
		Field[] fields= AnnotationUtil.getAllFields(field.getType());
		for(Field field1:fields) {
			if (field1.getAnnotation(Id.class) != null) {
				fk = field1.getName();
			}
		}


		if (vo == null) {
			org.example.codegenerate.modelext.WebVo.domian.column.PageVo page = column.getPageVo();

			page.setFieldDefaultValue("");
			page.setFieldLength(120);
			//page.setFieldShowType(vo.fieldShowType());


			page.setIsQuery(0);
			page.setIsReadOnly(0);
			if(field.getAnnotation(ManyToOne.class)!=null){
				page.setIsShowForm(0);
				page.setIsShowList(0);
				page.setFieldShowType(EnumPageFieldType.文本框.getType());
			}else {
				page.setIsShowForm(1);
				page.setIsShowList(1);
				EnumJavaMappingDbType enumJavaMappingDbType=EnumJavaMappingDbType.getDefaultPageTypeByJavaTypeFullName(field.getType().getName());
				if(enumJavaMappingDbType!=null){
					page.setFieldShowType(enumJavaMappingDbType.getPageType());
				}else {
					page.setFieldShowType(EnumPageFieldType.文本框.getType());
				}

			}
			page.setQueryMode("single");
			page.setSortFlag("N");
			page.setOrderNum(0);
			column.setPageVo(page);
		}

		return column;
	}
}
